package com.wakaka.controller;

import com.wakaka.pojo.Dept;
import com.wakaka.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供Rest ful服务
@RestController
public class DeptController {

    @Autowired
    IDeptService service;

    // 获得一些信息
    @Autowired
    DiscoveryClient client;

    @PostMapping("/add")
    public int add(@RequestBody String name) {
        return service.add(name);
    }

    @RequestMapping("/queryById/{id}")
    public Dept queryById(@PathVariable("id") long id) {
        return service.queryById(id);
    }

    @RequestMapping("/queryAll")
    public List<Dept> queryAll() {
        return service.queryAll();
    }

    // 从注册进来的微服务中获取消息
    @GetMapping("/getinfo")
    public Object discovery() {
        // 得到微服务列表的清单
        List<String> list = client.getServices();
        System.out.println("services:" + list);

        // 通过微服务ID得到具体的微服务信息
        List<ServiceInstance> provider = client.getInstances("PROVIDER");
        for (ServiceInstance instance : provider) {
            System.out.println(instance.getHost() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getUri()
            );
        }
        return client;
    }
}
